#!/usr/bin/env bash
# Copyright (c) 2013 Shotgun Software Inc.
# 
# CONFIDENTIAL AND PROPRIETARY
# 
# This work is provided "AS IS" and subject to the Shotgun Pipeline Toolkit 
# Source Code License included in this distribution package. See LICENSE.
# By accessing, using, copying or modifying this work you indicate your 
# agreement to the Shotgun Pipeline Toolkit Source Code License. All rights 
# not expressly granted therein are reserved by Shotgun Software Inc.

# get absolute location of this script
SELF_PATH=$(cd -P -- "$(dirname -- "$0")" && pwd -P) && SELF_PATH=$SELF_PATH/$(basename -- "$0")

# resolve symlinks
while [ -h "$SELF_PATH" ]; do
    # 1) cd to directory of the symlink
    # 2) cd to the directory of where the symlink points
    # 3) get the pwd
    # 4) append the basename
    DIR=$(dirname -- "$SELF_PATH")
    SYM=$(readlink "$SELF_PATH")
    SELF_PATH=$(cd "$DIR" && cd $(dirname -- "$SYM") && pwd)/$(basename -- "$SYM")
done

# chop off the file name
SELF_PATH=$(dirname "$SELF_PATH")


# first of all, for performance, check if the command is shotgun_get_actions
# syntax ./tank shotgun_get_actions cache_file_name env_yml_file_name
# returns 0 and outputs action cache contents to stdout on success
# returns 1 if the cache file is older than the yml file.
# returns 2 if the yml file does not exist
if [ -n "$1" ] && [ "$1" = "shotgun_get_actions" ]; then

    CACHE_FILE="$SELF_PATH/cache/$2"
    ENV_FILE="$SELF_PATH/config/env/$3"

    # return 2 if the yml file does not exist
    if [ ! -f "$ENV_FILE" ];
    then
        exit 2
    fi

    # return cache contents if cache is up to date
    if [ "$CACHE_FILE" -nt "$ENV_FILE" ]; then
        cat "$CACHE_FILE"
        exit 0
    else
        exit 1
    fi
fi


# if we have a local install of the core, this is the script to dispatch to
LOCAL_SCRIPT="$SELF_PATH/install/core/scripts/tank_cmd.sh"

# when called from shotgun, we reroute to a special script which uses a login shell shebang.
if [ -n "$1" ] && ( [ "$1" = "shotgun_run_action" ] || [ "$1" = "shotgun_cache_actions" ] ); then
    LOCAL_SCRIPT="$SELF_PATH/install/core/scripts/tank_cmd_login.sh"
fi

# set up an env var to track the current pipeline configuration
# this is to help the tank core API figure out for example tank.tank_from_path()
# when using multiple work dev areas.
# since we are recursing upwards, only set it if it is not already set.
# we only set this when it is a pipeline location. Check this by looking for
# the roots.yml file
ROOTS_FILE="$SELF_PATH/config/core/roots.yml"
if [ -z "$TANK_CURRENT_PC" ] && [ -f "$ROOTS_FILE" ]; then
    export TANK_CURRENT_PC=$SELF_PATH
fi


if [ -f "$LOCAL_SCRIPT" ];
then
   # run local script
   "$LOCAL_SCRIPT" "$SELF_PATH" "$@"
else
   # there is no local install
   # so try and get the parent and call its tank script
   # the parent location is stored in a config file
   curr_platform=`uname`
   if [[ "${curr_platform}" == MINGW64_NT* ]] || [[ "${curr_platform}" == MINGW32_NT* ]] ||
        [[ "${curr_platform}" ==  CYGWIN_NT* ]];
   then
       curr_platform="Windows"
   fi
   parent_config_file="$SELF_PATH/install/core/core_${curr_platform}.cfg"

   if [ ! -f "$parent_config_file" ];
   then
      echo "Cannot find parent configuration file $parent_config_file!"
      exit 1
   fi

   # now get path to parent
   # by eval-ing the contents we are expanding any environment variables that may
   # exist in the parent config file. For example, if the parent_config_file contains
   # $STUDIO_TANK_PATH, it will be expanded correctly assuming the env var exists.
   parent_location=$( eval echo $( cat "$parent_config_file" ) )
   # and check that it exists...
   if [ ! -d "$parent_location" ];
   then
      echo "Cannot find parent $parent_location defined in config file $parent_config_file!"
      exit 1
   fi

   # all good, execute tank script in parent location
   "$parent_location/tank" "$@" --pc="$SELF_PATH"

fi


